package com.wt.struggle.goodbye.main.fragment

import android.os.Bundle
import android.support.design.widget.TabLayout
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.ProV4Fragment
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.main.activity.SendTieActivity
import com.xin.lv.yang.utils.info.PicItem
import kotlinx.android.synthetic.main.photo_video_choose_layout.*
import org.devio.takephoto.app.TakePhoto
import org.devio.takephoto.model.TResult

/**
 * 选择图片  拍照 还是录制视频
 */
class ChoosePhotoFragment : ProV4Fragment() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {


    }

    override fun createView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.photo_video_choose_layout, container, false)

    }

    var arrFragment = arrayListOf<ProV4Fragment>(PhotoFragment(), CameraFragment(), VideoFragment())

    var takePhotoUtil: TakePhoto? = null

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        takePhotoUtil = takePhoto

        val tttt = childFragmentManager.beginTransaction()
        tttt.add(R.id.photoOrVideoFrameLayout, arrFragment[2])
        tttt.commit()

        videoOrPhotoTabLayout.getTabAt(2)!!.select()

        videoOrPhotoTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {
                when (tab!!.position) {
                    0 -> {
                        takePhotoUtil!!.onPickMultiple(9)
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val tttt = childFragmentManager.beginTransaction()
                when (tab!!.position) {

                    0 -> {

                        takePhotoUtil!!.onPickMultiple(9)

                    }

                    1 -> {

                        tttt.replace(R.id.photoOrVideoFrameLayout, arrFragment[1])
                        tttt.commit()
                    }

                    2 -> {

                        tttt.replace(R.id.photoOrVideoFrameLayout, arrFragment[2])
                        tttt.commit()
                    }
                }
            }
        })
    }


    override fun takeSuccess(result: TResult?) {
        super.takeSuccess(result)

        val arr = result!!.images
        val lll = arrayListOf<String>()

        for (temp in arr) {

            lll.add(temp.originalPath)
        }

        (activity as SendTieActivity).savePicList(lll)


    }
}